package com.xhwl.centiotdata.handler.alarm.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.centiotdata.handler.alarm.AlarmHandler;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.alarm.haiwei3800.AlarmContent;
import com.xhwl.common.dto.cent.alarm.iot.MaxAlarmBody;
import com.xhwl.common.dto.cent.alarm.iot.MaxDataBody;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.CodeEnum;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.alarm.AlarmConfig;
import com.xhwl.common.pojo.cent.alarm.AlarmLog;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import com.xhwl.common.utils.XMLUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Auther ChaoYun
 * @Description
 * @Date 2022/8/30
 */
@Component
@Slf4j
public class AlarmIStartIotMaxHandle extends AlarmHandler {
    @Override
    public void afterPropertiesSet() throws Exception {
    }

    public ResultJson maxInfoReceive(String receiveBody, Integer projectId) {
        AlarmLog alarmLog = new AlarmLog();
        alarmLog.setTag("iot-max");
        alarmLog.setContent(receiveBody);
        alarmLog.setPushTime(new Date());
        centAlarmService.insert(alarmLog);
        Map<String, String> entries = redisTemplate.opsForHash().entries(RedisUtils.format(61));
        if (StringUtils.isBlank(receiveBody)) {
            return  ResultJson.fail(CodeEnum.MISSING_REQUEST_PARAM_ERROR);
        }
        MaxDataBody maxDataBody = JSONObject.parseObject(receiveBody, MaxDataBody.class);
        List<MaxAlarmBody>  maxAlarmBodyList = maxDataBody.getAlarms();
        maxAlarmBodyList.forEach( alarmBody -> {
            if (StringUtils.isNotBlank(entries.get(String.valueOf(alarmBody.getAlarm_code())))) {
                DevicePageQuery devicePageQuery = new DevicePageQuery();
                devicePageQuery.setThirdDeviceId(maxDataBody.getMax_sn());
                Page<DeviceDTO> deviceDTOPage = deviceCentService.pageDevice(null, devicePageQuery);
                DeviceDTO deviceDTO = Optional.ofNullable(deviceDTOPage).map(Page::getRecords)
                        .filter(item -> CollectionUtils.isNotEmpty(item))
                        .map(item -> item.get(0)).orElseThrow(() -> new DataException("====================》设备不存在《================"));
                ThreadPoolUtils.scheduledExecutorService.schedule(() -> {
                AlarmManagement alarmManagement = new AlarmManagement();
                alarmManagement.setAlarmTime(new Date());
                alarmManagement.setDeviceId(deviceDTO.getId());
                alarmManagement.setProjectId(deviceDTO.getProjectId());
                alarmManagement.setContent(alarmBody.getAlarm_message());
                alarmManagement.setAlarmInventoryCode(entries.get(String.valueOf(alarmBody.getAlarm_code())));
                sendMq(AlarmConstant.ALARM_TOPIC, alarmManagement);
                }, 5, TimeUnit.SECONDS);
            }
        });
        return ResultJson.success();
    }

}
